# -*- coding: utf-8 -*-            
# @Author : chenliang
# @Time : 2024/3/13 11:35

import time
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.admin_data_storage_subscription_locator import AdminDataStorageLocator
from Case.python_ui.Test_data.menu_name_data import MenuName
import re
from Case.python_ui.Common.excel import excel
from Case.python_ui.Test_data.login_data import Logindata
from Case.python_ui.PageLocate.admin_ticket_management_locator import AdminTicketManagementLocator
from Case.python_ui.Common.business_common_tool import BusinessCommonTools
from selenium.common.exceptions import NoSuchElementException
from datetime import datetime


class AdminDataStorage(Basepage):
    """
        公共方法
    """

    # 判断一个列表中的所有值是否与给定的字符串完全一致
    def all_equal_to_string(self,lst, string):
        return all(elem == string for elem in lst)

    # 时间格式01-03-2024 15:37:00 转化为 2024-03-01
    def convert_date_format(self,date_str):
        # 将输入的时间字符串解析为 datetime 对象
        date_obj = datetime.strptime(date_str, "%d-%m-%Y %H:%M:%S")
        # 将 datetime 对象转换为所需的日期格式字符串
        new_date_str = date_obj.strftime("%Y-%m-%d")
        return new_date_str


    """
        Data Storage Subscription | Subscription History
    """

    # 等待页面加载转动
    def wait_web_page_rotate(self, time=100):
        doc = '等待页面加载转动图标'
        try:
            self.wait_elevisble(AdminDataStorageLocator.wait, doc, time)
            self.wait_eleinvisble(AdminDataStorageLocator.wait, doc, time)
        except:
            pass

    # 进入Data Storage Subscription | Subscription History页面
    def enter_subscription_history(self):
        doc = '进入Subscription History页面'
        time.sleep(5)
        self.scroll_bar_operation(AdminDataStorageLocator.data_storage_subscription_menu)
        self.move_mouse_to_click_element(AdminDataStorageLocator.data_storage_subscription_menu,doc)
        time.sleep(2)
        # try:
        #     text = self.get_attribute(AdminDataStorageLocator.if_open_data_storage_subscription, 'Class', doc)
        # except NoSuchElementException as e:
        #     text = 'antd4-menu antd4-menu-sub antd4-menu-inline antd4-menu-hidden'
        text = self.get_attribute(AdminDataStorageLocator.if_open_data_storage_subscription, 'Class', doc)
        if text == 'antd4-menu antd4-menu-sub antd4-menu-inline antd4-menu-hidden':
            self.move_mouse_to_click_element(AdminDataStorageLocator.data_storage_subscription_menu, doc)
        self.wait_elevisble(AdminDataStorageLocator.subscription_history_menu, doc)
        self.move_mouse_to_click_element(AdminDataStorageLocator.subscription_history_menu, doc)
        self.wait_web_page_rotate(time=10)
        self.wait_elevisble(AdminDataStorageLocator.subscription_history_page_text, doc)
        assert self.get_element(AdminDataStorageLocator.subscription_history_page_text,
                                doc).text == 'Subscription History'

    # 查询列表第一条数据，2 Operator Email ,3 Subscription Name ,无值返回0
    def get_list_first_context(self, id):
        doc = '获取列表第一条数据的值'
        time.sleep(2)
        try:
            self.wait_elevisble(AdminDataStorageLocator.get_list_first(id), doc)
            first_context = self.get_element(AdminDataStorageLocator.get_list_first(id), doc).text
            return first_context
        except:
            return '0'

    # 根据邮箱去进行查询，返回列表所有邮箱的值
    def search_by_email(self):
        doc = '根据邮箱进行查询'
        email = self.get_list_first_context('2')
        if email != '0':
            context1, context2 = AdminDataStorageLocator.choose_by_name('Operator Email', 'email')
            self.wait_elevisble(context2, doc)
            self.send_keys(context2, email, doc)
            self.click_element(AdminDataStorageLocator.click_search, doc)
            time.sleep(2)
            email_list = []
            email_list_old = self.get_elements(AdminDataStorageLocator.email_list, doc)
            for i in email_list_old:
                email_list.append(i.text)
            text = '实际值:' + ','.join(email_list) + '预期值:' + email
            assert self.all_equal_to_string(email_list, email), text
        else:
            text = '列表无数据'
            assert 1 == 2, text

    # 根据subscription_name去进行查询，返回列表所有邮箱的值
    def search_by_subscription_name(self):
        doc = '根据subscription_name进行查询'
        subscription_name = self.get_list_first_context('3')
        time.sleep(2)
        self.wait_elevisble(AdminDataStorageLocator.click_input_type,doc)
        self.move_mouse_to_click_element(AdminDataStorageLocator.click_input_type,doc)
        time.sleep(2)
        context1, context2 = AdminDataStorageLocator.choose_by_name('Subscription Name', 'fileSubscribeName')
        self.wait_elevisble(context1,doc)
        self.move_mouse_to_click_element(context1,doc)
        if subscription_name != '0':
            self.wait_elevisble(context2, doc)
            self.send_keys(context2, subscription_name, doc)
            self.click_element(AdminDataStorageLocator.click_search, doc)
            time.sleep(2)
            email_list = []
            email_list_old = self.get_elements(AdminDataStorageLocator.subscription_name_list, doc)
            for i in email_list_old:
                email_list.append(i.text)
            text = '实际值:' + ','.join(email_list) + '预期值:' + subscription_name
            assert self.all_equal_to_string(email_list, subscription_name), text
        else:
            text = '列表无数据'
            assert 1 == 2, text


    # 清空时间选项，选择特定时间，返回数量
    def clear_time_select_choose_time(self, select_time):
        doc = '清空时间选项，选择特定时间'
        time.sleep(2)
        try:
            self.move_mouse_to_click_element(AdminDataStorageLocator.clear_time, doc)
        except:
            pass
        try:
            self.wait_elevisble(AdminDataStorageLocator.wait, doc, timeout=10)
            self.wait_eleinvisble(AdminDataStorageLocator.wait, doc, timeout=10)
        except:
            pass
        self.wait_elevisble(AdminDataStorageLocator.order_time, doc)
        self.click_element(AdminDataStorageLocator.order_time, doc)
        for i in range(12):
            try:
                self.wait_elevisble(AdminDataStorageLocator.select_time(select_time), doc, timeout=10)
                break
            except:
                self.move_mouse_to_click_element(AdminDataStorageLocator.choose_time_before, doc)
                continue
        self.click_element(AdminDataStorageLocator.select_time(select_time), doc)
        self.click_element(AdminDataStorageLocator.select_time(select_time), doc)
        time.sleep(2)
        self.click_element(AdminDataStorageLocator.click_search, doc)
        time.sleep(5)
        try:
            self.wait_elevisble(AdminDataStorageLocator.total_text, doc)
            total_text = self.get_element(AdminDataStorageLocator.total_text, doc).text
            # 正则表达式拆分total
            pattern = r"[ ]"  # 模式表示逗号、感叹号、问号或空格出现一次或多次
            total_result = re.split(pattern, total_text)
            total = int(total_result[1])
        except:
            total = 0
        print('根据时间查询页面数量：', total)
        self.save_screen(doc)
        return total

    # 获取第一例数据的time值
    def get_list_first_time(self):
        doc = '获取第一条数据的time值并返回'
        time.sleep(2)
        try:
            self.wait_elevisble(AdminDataStorageLocator.get_first_time, doc)
            first_context = self.get_element(AdminDataStorageLocator.get_first_time, doc).text
            first_context=self.convert_date_format(first_context)
            return first_context
        except:
            return '0'

    # 根据时间查询，交验查询数据time值是否正确
    def check_time(self,select_time='2023-11-23'):
        doc = '根据时间进行查询，查询交验结果'
        first_time=self.get_list_first_time()
        #first_time=select_time
        if first_time !='0':
            total=self.clear_time_select_choose_time(first_time)
            time.sleep(2)
            time_new = []
            time_list=self.get_elements(AdminDataStorageLocator.time_list,doc)
            for i in time_list:
                time_new.append(self.convert_date_format(i.text))
            text = '实际值:' + ','.join(time_new) + '预期值:' + first_time
            assert self.all_equal_to_string(time_new, first_time), text
        else:
            text = '列表无数据'
            assert 1 == 2, text




